package com.lingdong.onlinejudge.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingdong.onlinejudge.annotation.AuthCheck;
import com.lingdong.onlinejudge.common.BaseResponse;
import com.lingdong.onlinejudge.common.ErrorCode;
import com.lingdong.onlinejudge.common.ResultUtils;
import com.lingdong.onlinejudge.exception.BusinessException;
import com.lingdong.onlinejudge.model.dto.topic.TopicAddDto;
import com.lingdong.onlinejudge.model.dto.topic.TopicBindDto;
import com.lingdong.onlinejudge.model.dto.topic.TopicQueryDto;
import com.lingdong.onlinejudge.model.dto.topic.TopicUpdateDto;
import com.lingdong.onlinejudge.model.vo.topic.TopicListVo;
import com.lingdong.onlinejudge.model.vo.topic.TopicVo;
import com.lingdong.onlinejudge.service.TopicService;
import net.sf.jsqlparser.statement.select.Top;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 题目
 * @author lzw
 */
@RestController
@RequestMapping("/topic")
public class TopicController {

    @Resource
    private TopicService topicService;

    /**
     * 添加题目
     * @param topicAddDto 题目
     * @param token token
     * @return 结果
     */
    @AuthCheck(mustRole = "admin")
    @PostMapping("/add")
    public BaseResponse<Boolean> addTopic(@RequestBody TopicAddDto topicAddDto,
                                          @RequestHeader("token") String token){
        if (topicAddDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = topicService.addTopic(topicAddDto, token);
        return ResultUtils.success(b);
    }

    /**
     * 更新题目
     * @param topicUpdateDto 题目
     * @return 结果
     */
    @AuthCheck(mustRole = "admin")
    @PutMapping("/update")
    public BaseResponse<Boolean> updateTopic(@RequestBody TopicUpdateDto topicUpdateDto){
        if (topicUpdateDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = topicService.updateTopic(topicUpdateDto);
        return ResultUtils.success(b);
    }

    /**
     * 获取题目
     * @param id 题目id
     * @param token token
     * @return 结果
     */

    @GetMapping("/get/{id}")
    public BaseResponse<TopicVo> getTopic(@PathVariable("id") Long id,
                                          @RequestHeader("token") String token){
        TopicVo topicAddDto = topicService.getTopic(id,token);
        return ResultUtils.success(topicAddDto);
    }

    /**
     * 查询题目
     * @param topicName 题目名
     * @return 结果
     */
    @GetMapping("/query/list")
    public BaseResponse<List<TopicListVo>> queryTopicList(@RequestParam String topicName){
        List<TopicListVo> topicAddDto = topicService.queryTopicList(topicName);
        return ResultUtils.success(topicAddDto);
    }

    /**
     * 分页查询题目
     * @param topicQueryDto 题目
     * @param token token
     * @return 结果
     */
    @GetMapping("/query/topic")
    public BaseResponse<Page<TopicVo>> getTopic(@RequestBody TopicQueryDto topicQueryDto,
                                                @RequestHeader("token") String token){
        if (topicQueryDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<TopicVo> topicAddDto = topicService.queryTopicByPage(topicQueryDto,token);
        return ResultUtils.success(topicAddDto);
    }

    /**
     * 删除题目
     * @param id 题目id
     * @return 结果
     */
    @AuthCheck(mustRole = "admin")
    @DeleteMapping("/delete")
    public BaseResponse<Boolean> deleteTopic(@RequestParam("id") Long id){
        if (id == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = topicService.deleteById(id);
        return ResultUtils.success(b);
    }

    /**
     * 绑定题目
     * @param topicBindDto 题目
     * @param token token
     * @return 结果
     */
    @AuthCheck(mustRole = "admin")
    @PostMapping("/bind")
    public BaseResponse<Boolean> bindTopic(@RequestBody TopicBindDto topicBindDto,
                                          @RequestHeader("token") String token){
        if (topicBindDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = topicService.bindTopic(topicBindDto, token);
        return ResultUtils.success(b);
    }



}
